/*
*    Phantom v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
// DataString.cpp: implementation of the CDataString class.
//
//////////////////////////////////////////////////////////////////////

#include "ssi_datastring.h"
#include "ssi_datastring.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

SSI_SCRIPTDLL_API SSI_INT64 CDataString::sType = 0;


CDataString::CDataString()
{
  Data = "";

	if(!sType){
		sType = 1;
		sType = Factory_RegisterData((CData*)new CDataString());
	}
  Type = sType;
	DataName = "string";
}

CDataString::~CDataString()
{

}

SSI_BOOL CDataString::CopyData(CData *Other)
{
  if(Type != Other->Type) return 0;
  if(Other == SSI_NULL) return 0;
  CDataString* Temp = (CDataString*)Other;
  Data = Temp->Data;
  return 1;
}

SSI_BOOL CDataString::DataFromString(const String& sData)
{
  // Maybe do a check for quotes here?
  Data = sData;
	if(!Data.CheckEndQuotes()){
		Data = "";
		return 0;
	}
  MakeRaw();
  return 1;
}

String CDataString::GetString()
{
  // Maybe add quotes to the data here?  Or strip them?  Dunno...
  return Data;
}

SSI_INT64 CDataString::SizeOf()
{
  return Data.GetLength()+1;  // To compensate for ending SSI_NULL
}

void CDataString::MakeRaw()
{
	char sp[2];
	sp[0] = 1;
	sp[1] = 0;
  Data.CheckEndQuotes(1);
	Data.Replace("\\\\", sp);
  Data.Replace("\\t", "\t");
  Data.Replace("\\n", "\n");
  Data.Replace("\\r", "\r");
  Data.Replace("\\\"", "\"");
	Data.Replace(sp, "\\");

}

void CDataString::MakeFormatted()
{
  Data.CheckEndQuotes(1);
  Data.Replace("\t", "\\t");
  Data.Replace("\n", "\\n");
  Data.Replace("\r", "\\r");
  Data.Replace("\"", "\\\"");
  Data.Replace("\\", "\\\\");
}

SSI_INT64 CDataString::GetRegisteredType(void)
{
	if(!sType){
		CDataString aData;
	}
	return sType;
}
CData* CDataString::operator_plus(CData* InData, OpInfoT* Info)
{
	if(InData->GetType() != Type) return SSI_NULL;
	Data=Data+((CDataString*)InData)->Data;
	return (CData*)this;
}

CData* CDataString::operator_equal(CData* InData, OpInfoT* Info)
{
	if(InData->GetType() != Type) return SSI_NULL;
	Data = ((CDataString*)InData)->Data;
	return (CData*)this;
}

CStatus CDataString::operator_compare(CData* InData, SSI_INT64& Result)
{
  if(InData->GetType() != Type){
    return CData::operator_compare(InData, Result);
  }
	CDataString* In = (CDataString*)InData;

	if(Data == In->Data){
		Result = 0;
	}else{
		Result = 1;
	}
	return STAT_OK;
}

SSI_INT64 CDataString::Register(SSI_INT64 NewType){
	sType = NewType;
	return 1;
}

void CDataString::UnRegister(){
	sType = 0;
}



